<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>加班申请</title>
    <script src="../../../../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../../../../res/js/login_common.js" charset="utf-8"></script>
    <script src="../../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../../res/js/Tools.js" charset="utf-8"></script>
    <link rel="shortcut icon" type="image/x-icon" href="../../../../../res/images/favicon.ico">
    <link rel="stylesheet" href="../../../../../res/layui/css/layui.css" media="all">
</head>
<body style="padding: 10px">
<div style="display: none" id="bodys">
    <form action="" class="layui-form layui-form-pane" lay-filter="example">
        <h1 align="center" style="margin-bottom: 20px">加班申请表</h1>
        <!--流程id-->
        <input name="pkBatchOvertimeId" id="pkBatchOvertimeId" type="hidden"/>
        <!--流程进度-->
        <input name="isRevision" id="isRevision" type="hidden"/>
        <!--员工id-->
        <input name="personnelId" id="personnelId" type="hidden"/>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="personnelName"
                           id="personnelName" placeholder="请输入名字（必填）" disabled=""
                           type="text"/>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">部门</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" id="departmentName"
                           name="departmentName" placeholder="请输入部门（必填）" disabled=""
                           type="text"/>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">职务</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" id="postName"
                           name="postName" placeholder="请输入岗位（必填）" disabled=""
                           type="text"/>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">加班时段</label>
                <div class="layui-input-inline" style="width: 300px">
                    <input type="text" class="layui-input" id="test10" placeholder=" - " disabled>
                </div>
                <label class="layui-form-label" style="width: 155px">加班工时合计(小时)</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" id="batchOvertimeDetailHour" name="batchOvertimeDetailHour"
                           disabled>
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">加班事由</label>
            <div class="layui-input-block">
            <textarea placeholder="请输入内容" disabled class="layui-textarea" lay-verify="required"
                      id="batchOvertimeDetailCause">    </textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text" id="department">
            <label class="layui-form-label">部门主管意见</label>
            <div class="layui-input-block">
            <textarea placeholder="请输入内容(不通过的话请不要在此填写内容，直接点击不通过)" class="layui-textarea" name="departmentOpinion"
                      id="departmentOpinion"
                      lay-verify="required">   </textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text" id="personnel" style="display: none">
            <label class="layui-form-label">人事部意见</label>
            <div class="layui-input-block">
            <textarea placeholder="请输入内容(不通过的话请不要在此填写内容，直接点击不通过)" class="layui-textarea" name="personnelOpinion"
                      id="personnelOpinion">
            </textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text" id="manager" style="display: none">
            <label class="layui-form-label">总经理意见</label>
            <div class="layui-input-block">
            <textarea placeholder="请输入内容" class="layui-textarea" name="managerOpinion" id="managerOpinion">
            </textarea>
            </div>
        </div>

        <!-- 提交-->
        <div class="layui-form-item" id="sub">
            <div class="layui-input-block" id="flowTwo">
                <button type="submit" class="layui-btn site-demo-active" lay-submit="" lay-filter="demo1">通过</button>
                <input type="button" id="demo3" class="layui-btn layui-btn-primary site-demo-active" value="不通过"/>
            </div>
        </div>
    </form>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>流程日志</legend>
    </fieldset>
    <ul class="layui-timeline" id="schedule">

    </ul>
</div>
<script>
    let pkBatchOvertimeId = getQueryString("pkBatchOvertimeId");
    layui.use(['form', 'laydate', 'table'], function () {
        var table = layui.table
            , form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate;

        //日期时间范围
        /*laydate.render({
            elem: '#test10'
            ,type: 'datetime'
            ,range: true
            ,format: 'yyyy年MM月dd日HH时'
            ,done: function(value, date, endDate){
                //console.log(value); //得到日期生成的值，如：2017-08-18
                //console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
                //console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
                let dates=date.year+'-'+date.month+'-'+date.date+' ';// yyyy-MM-dd HH:mm:ss
                if(date.hours>9){
                    dates+=date.hours+':00:00';
                }else{
                    dates+='0'+date.hours+':00:00';
                }
                let endDates=endDate.year+'-'+endDate.month+'-'+endDate.date+' ';
                if(endDate.hours>9){
                    endDates+=endDate.hours+':00:00';
                }else{
                    endDates+='0'+endDate.hours+':00:00';
                }
                console.info(dates+"   "+endDates);
                let sDate=new Date(dates);
                let sendDate=new Date(endDates);
                $("#batchOvertimeDetailStart").val(dates);
                $("#batchOvertimeDetailEnd").val(endDates);
                let batchOvertimeDetailHour=(sendDate-sDate)/1000/60/60;
                $("#batchOvertimeDetailHour").val(batchOvertimeDetailHour);
            }
        });*/

        let res = getAjaxById(getRootPath() + "OaBatchOvertimeFlowAction/findById/" + pkBatchOvertimeId);
        //流程id
        $("#pkBatchOvertimeId").val(res.pkBatchOvertimeId);
        //流程进度
        $("#isRevision").val(res.isRevision);
        //申请人
        $("#personnelId").val(res.personnelId);

        let date = new Date(res.batchOvertimeDetailStart);

        let dateEnd = new Date(res.batchOvertimeDetailEnd);

        let batchOvertimeDetail = date.getFullYear() + "年" + (date.getUTCMonth() + 1) + "月" + date.getDate() + "日" + date.getHours() + "时到" +
            dateEnd.getFullYear() + "年" + (dateEnd.getUTCMonth() + 1) + "月" + dateEnd.getDate() + "日" + dateEnd.getHours() + "时";
        console.info(batchOvertimeDetail);
        //加班区间
        $("#test10").val(batchOvertimeDetail);
        //小时数
        $("#batchOvertimeDetailHour").val(res.batchOvertimeDetailHour);
        //加班事由
        $("#batchOvertimeDetailCause").val(res.batchOvertimeDetailCause);
        console.info(res);
        let ress = getAjaxById(getRootPath() + "OaPersonnelAction/findById/" + res.personnelId);
        console.info(ress);
        // 员工名称
        $("#personnelName").val(ress.data.personnelName);
        // 部门
        $("#departmentName").val(ress.data.departmentName);
        // 岗位
        $("#postName").val(ress.data.postName);
        if (res.isRevision === "D") { // 部门审核
            auditSectionNode(res);
        } else if (res.isRevision === "R") { // 人事审核
            auditStaffingNode(res);
        } else if (res.isRevision === "E") { // 总经理审核
            auditExecutiveNode(res)
        } else if (res.isRevision === "1") { // 查看
            showOvertime(res);
        }

        /*let data=getAjaxById(getRootPath()+"OaRegularFlowAction/isExist");
        if(data){
            //墨绿深蓝风
            layer.alert('你已提交转正申请请勿重复提交', {
                skin: 'layui-layer-molv' //样式类名
                ,closeBtn: 0
            }, function(){
                formClose();
                parent.layui.table.reload('test');//刷新父页面
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });
        }*/

        //监听提交
        form.on('submit(demo1)', function (data) {
            console.info(data.field);
            //部门审核通过
            let url = getRootPath() + "OaBatchOvertimeFlowAction/sectionPass";
            let isRevision = $("#isRevision").val();
            if (isRevision === "R") { // 人事审核通过
                url = getRootPath() + "OaBatchOvertimeFlowAction/staffingPass";
            } else if (isRevision === "E") { //总经理审核通过
                url = getRootPath() + "OaBatchOvertimeFlowAction/exPass";
            }

            /*data={
                affirmTime:$("#affirmTime").val(),
                personnelId:$("#personnelId").val()
            };*/
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            const res = postAjax(url, data.field);
            if (res.code === 1) {
                layer.msg('新增成功3秒后关闭当前窗口', {icon: 1});
                setTimeout(function () {
                    formClose();
                }, 3000);
                setTimeout(function () {
                    parent.layui.table.reload('test');//刷新父页面
                    //window.parent.location.reload();//刷新父页面
                    const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }, 3000);
                //window.location.reload();//刷新页面
            } else if (res.code === 500) {
                error('500');
            } else {
                layer.msg("新增失败");
            }
            return false;
        });

        //监听审核提交不通过
        $('#demo3').on('click', function () {
            //prompt层
            layer.prompt({
                formType: 2,
                value: '',
                title: '请输入原因',
                area: ['300px', '100px']
            }, function (text, index) {
                layer.close(index);
                let datas = {
                    formReason: text,
                    pkBatchOvertimeId: $("#pkBatchOvertimeId").val(),
                    personnelId: $("#personnelId").val()
                };
                // 单击之后提交按钮不可选,防止重复提交
                const DISABLED = 'layui-btn-disabled';
                let site_demo_active = $('.site-demo-active');
                // 增加样式
                site_demo_active.addClass(DISABLED);
                // 增加属性
                site_demo_active.attr('disabled', 'disabled');
                let isRevision = $("#isRevision").val();
                let url = getRootPath() + "OaBatchOvertimeFlowAction/sectionNoPass";
                if (isRevision === "R") {
                    url = getRootPath() + "OaBatchOvertimeFlowAction/staffingNoPass";
                } else if (isRevision === "E") {
                    url = getRootPath() + "OaBatchOvertimeFlowAction/exNoPass";
                }
                /*layer.msg('您最后写下了：'+text);*/
                const res = postAjax(url, datas);
                if (res.code === 1) {
                    layer.msg('提交成功3秒后关闭当前窗口', {icon: 1});
                    setTimeout(function () {
                        formClose();
                    }, 3000);
                    setTimeout(function () {
                        parent.layui.table.reload('test');//刷新父页面
                        //window.parent.location.reload();//刷新父页面
                        const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
                    }, 3001);
                } else if (res.code === 500) {
                    error('500');
                } else {
                    layer.msg("新增失败");
                }
            });
            return false;
        });

        if (pkBatchOvertimeId) {
            const ress = getAjaxById(getRootPath() + "OaFlowAction/selectFlowSchedule/" + pkBatchOvertimeId);
            $("#schedule").html("");
            $.each(ress, function (index, item) {
                if (item.createdBy) {
                    $("#schedule").append('<li class="layui-timeline-item">\n' +
                        '        <i class="layui-icon layui-timeline-axis"></i>\n' +
                        '        <div class="layui-timeline-content layui-text">\n' +
                        '            <h3 class="layui-timeline-title">' + dateFormat('yyyy-MM-dd hh:mm:ss', new Date(item.createdTime)) + '&nbsp;&nbsp;&nbsp;' + item.flowName + '</h3>\n' +
                        '            <p>' + item.formReason + '</p>\n' +
                        '            <p>操作人：' + item.createdBy + '</p>\n' +
                        '        </div>\n' +
                        '    </li>')

                } else {
                    $("#schedule").append('<li class="layui-timeline-item">\n' +
                        '        <i class="layui-icon layui-timeline-axis"></ i>\n' +
                        '        <div class="layui-timeline-content layui-text">\n' +
                        '            <h3 class="layui-timeline-title">' + dateFormat('yyyy-MM-dd hh:mm:ss', new Date(item.createdTime)) + '&nbsp;&nbsp;&nbsp;' + item.flowName + '</h3>\n' +
                        '            <p>' + item.formReason + '</p>\n' +
                        '        </div>\n' +
                        '    </li>')
                }
            });
        }


        /*if (isButton("ENTRY_CHECK") && $("#isRevision").val() === "L") {
            $("#entryCheck").show();
            $("#trialDate").attr("lay-verify", "required");
            $("#trialMoney").attr("lay-verify", "required");
            $("#officialMoney").attr("lay-verify", "required");
        }*/
        $("#bodys").show();
        form.render();
    });

    /**
     * 禁用
     *  @param forms 控件
     */
    function formDisabled(forms) {
        const DISABLED = 'layui-btn-disabled';
        forms.attr('disabled', 'disabled');
        forms.addClass(DISABLED);
    }

    /**
     * 查看
     * @param data 流程模型
     **/
    function showOvertime(data) {
        auditExecutiveNode(data);
        let managerOpinion=$("#managerOpinion");
        managerOpinion.val(data.managerOpinion);
        //禁用人事审核
        formDisabled(managerOpinion);
        $("#sub").hide();
    }

    /**
     * 总经理审核
     *  @param data 流程模型
     */
    function auditExecutiveNode(data) {
        auditStaffingNode(data);
        $("#manager").show();
        let personnelOpinion=$("#personnelOpinion");
        personnelOpinion.val(data.personnelOpinion);
        $("#managerOpinion").attr("lay-verify", "required");
        //禁用人事审核
        formDisabled(personnelOpinion);
    }

    /**
     * 人事审核
     *  @param data 流程模型
     */
    function auditStaffingNode(data) {
        auditSectionNode(data);
        //显示人事审核输入框
        $("#personnel").show();
        //设置人事审核为必填
        $("#personnelOpinion").attr("lay-verify", "required");
        let departmentOpinion = $("#departmentOpinion");
        departmentOpinion.val(data.departmentOpinion);
        //禁用部门审核
        formDisabled(departmentOpinion);
    }

    /**
     * 部门审核
     * @param data 流程模型
     **/
    function auditSectionNode(data) {

    }

    /**
     * 禁用
     *  @param forms 控件
     */
    function formDisabled(forms) {
        const DISABLED = 'layui-btn-disabled';
        forms.attr('disabled', 'disabled');
        forms.addClass(DISABLED);
    }
</script>
</body>
</html>